import {
  getRoleList,
  getRoleJobs,
  getRoleCampus,
  checkRole,
  updateRoleStatus,
  getRoleUserInfo,
  addRoleUser,
  getTeacherList,
} from  '../../service/api';
import { myMessage } from '../../utils/formatDataSource';

export default {
  namespace: 'role',

  state: {
    users: [],
    jobs: [],
    campus: [],
    existLesson: [],
    userInfo: {},
    teachers: {
      total: 0,
      data: [],
    },
  },

  effects: {
    *teacherList({ payload }, { call, put }) {
      const response = yield call(getTeacherList, payload);
      yield put({
        type: 'r_teacherList',
        payload: response,
      });
    },
    *personList({ payload }, { call, put }) {
      const response = yield call(getRoleList, payload);
      yield put({
        type: 'r_personList',
        payload: response,
      });
    },
    *jobs({ payload }, { call, put }) {
      const response = yield call(getRoleJobs, payload);
      yield put({
        type: 'r_jobs',
        payload: response,
      });
    },
    *campus({ payload }, { call, put }) {
      const response = yield call(getRoleCampus, payload);
      yield put({
        type: 'r_campus',
        payload: response,
      });
    },
    *check({ payload }, { call, put }) {
      const response = yield call(checkRole, payload);
      yield put({
        type: 'r_check',
        payload: response,
      });
    },
    *update({ payload }, { call, put }) {
      const response = yield call(updateRoleStatus, payload);
      yield put({
        type: 'r_update',
        payload: response,
      });
    },
    *userInfo({ payload }, { call, put }) {
      const response = yield call(getRoleUserInfo, payload);
      yield put({
        type: 'r_userInfo',
        payload: response,
      });
    },
    *addUser({ payload }, { call, put }) {
      const response = yield call(addRoleUser, payload);
      yield put({
        type: 'r_addUser',
        payload: response,
      });
    },
  },

  reducers: {
    r_teacherList(state, action) {
      if (action.payload) {
        const { data } = action.payload;
        data.data.map(d => {
          const arr = d.teacherTimeVo;
          arr.map(a => {
            if (a.type === 1) {
              d.cTime = a.typeTime;
            } else if (a.type === 2) {
              d.v1 = a.typeTime;
            } else if (a.type === 3) {
              d.vv1 = a.typeTime;
            }
          });
        });
        state.teachers = data;
      }
      return {
        ...state,
      };
    },
    r_personList(state, action) {
      if (action.payload) {
        const { status, data = [] } = action.payload;
        state.users = data;
      }
      return {
        ...state,
      };
    },
    r_jobs(state, action) {
      if (action.payload) {
        const { data = [] } = action.payload;
        state.jobs = data;
      }
      return {
        ...state,
      };
    },
    r_campus(state, action) {
      if (action.payload) {
        const { data = [] } = action.payload;
        state.campus = data;
      }
      return {
        ...state,
      };
    },
    r_check(state, action) {
      if (action.payload) {
        const { data = [] } = action.payload;
        state.existLesson = data;
      }
      return {
        ...state,
      };
    },
    r_update(state, action) {
      const { status } = action.payload;

      if (status === 0) {
        myMessage.success('操作成功', 2);
      } else {
        myMessage.error('操作失败', 2);
      }
      return {
        ...state,
      };
    },
    r_userInfo(state, action) {
      if (action.payload) {
        const { data = {} } = action.payload;
        state.userInfo = data;
      }
      return {
        ...state,
      };
    },
    r_addUser(state, action) {
      const { status } = action.payload;
      if (status === 0) {
        myMessage.success('操作成功', 2);
      } else if (status === 1) {
        myMessage.error(action.payload.errorMsg, 2);
      }
      return {
        ...state,
      };
    },
  },
};
